package com.qfedu.rocketmq01.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/13 11:42
 * @Modified:
 */
@RestController
@RequestMapping("api/rocketmq")
@Slf4j
@Tag(name = "普通消息发送")
public class MqSendController {

    /**
     * 自动创建 对象，只需注入即可*/
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("sendmsg1")
    public String sendMsg1(String msg){
        //发送消息  同步发送
        //不获取发送的结果
        //convertAndSend 参数说明：1.主题  2.消息
        //Message message= MessageBuilder.createMessage(msg,null);
        rocketMQTemplate.convertAndSend("xingmq01",msg);
        return "OK";
    }
    @GetMapping("sendmsg2")
    public String sendMsg2(String msg){
        //发送消息  同步发送
        SendResult result=rocketMQTemplate.syncSend("xingmq01",msg,5000);
        log.info("同步发送消息，结果={}",result.getSendStatus().name());

        return "OK";
    }
    @GetMapping("sendmsg3")
    public String sendMsg3(String msg){
        //发送消息  同步发送
        rocketMQTemplate.asyncSend("xingmq01", msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("异步发送消息，结果={}",sendResult.getSendStatus().name());
            }

            @Override
            public void onException(Throwable throwable) {

            }
        });
        return "OK";
    }
    @GetMapping("sendmsg4")
    public String sendMsg4(String msg){
        //发送消息
        rocketMQTemplate.sendOneWay("xingmq01",msg);
        return "OK";
    }
}
